﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES;
using GestionPedago.Services.Mapping;
using GestionPedago.Persistence.OPManager;
using GestionPedago.EntityContext;
using GestionPedago.Services.ES.Modele;

namespace GestionPedago.Services.Traitement
{
    /// <summary>
    /// Singleton
    /// </summary>
    public class PlanifierPlanningECFDetailTraitement : ITraitement<PlanifierPlanningECFDetailIn, PlanifierPlanningECFDetailOut>
    {
        /// <summary>
        /// Singleton
        /// </summary>
        private static PlanifierPlanningECFDetailTraitement instance;
        
        /// <summary>
        /// Méthode GetInstance
        /// </summary>
        /// <returns>Le singleton</returns>
        public static PlanifierPlanningECFDetailTraitement GetInstance()
        {
            if (instance == null)
            {
                instance = new PlanifierPlanningECFDetailTraitement();
            }
            return instance;
        }

        /// <summary>
        /// Constructeur
        /// </summary>
        private PlanifierPlanningECFDetailTraitement() { }

        /// <summary>
        /// Méthode de traitement
        /// </summary>
        /// <param name="paramEntree">Le paramètre d'entrée du service</param>
        /// <returns>Le paramètre de sortie du service</returns>
        public PlanifierPlanningECFDetailOut Traitement(PlanifierPlanningECFDetailIn paramEntree)
        {

            List<PlanningECFDetail> lstAvantMiseAJour = new List<PlanningECFDetail>();
            PlanningIndividuelFormation pif = PlanningIndividuelFormationOPManager.GetPlanningIndividuelFormationByCode(paramEntree.PlanningIndividuelFormation.CodePlanning);
            EntityConnection.GetContext().PlanningIndividuelFormation.Detach(pif);
            lstAvantMiseAJour = pif.GetListePlanificationsECFDetail();
            List<PlanningECFDetailES> lstRetour = new List<PlanningECFDetailES>();
            EntityConnection.Reinitialisation();
            foreach (PlanningECFDetailES pede in paramEntree.ListePlanningECFDetail)
            {
                PlanningECFDetail ped;
                if (pede.DejaPlanifie == true)
                {
                    if (pede.IdPlanningECFDetail != 0)
                    {

                        if (pede.PlanningECF.DateECF != pede.DateSuggeree)
                        {
                            ECFES ee = pede.PlanningECF.ECF;
                            pede.PlanningECF = new PlanningECFES();
                            pede.PlanningECF.CodePlanningECF = 0;
                            pede.PlanningECF.DateECF = (DateTime)pede.DateSuggeree;
                            pede.PlanningECF.ECF = ee;
                            pede.PlanningECF.IdECF = ee.IdECF;
                        }

                        ped = PlanningECFDetailOPManager.MiseAJourPlanningECFDetail(new PlanningECFDetailMappeur().ES2Entity(pede), new ECFMappeur().ES2Entity(pede.PlanningECF.ECF),pede.PromotionSuggeree);


                    }
                    else
                    {
                        ped = new PlanningECFDetailMappeur().ES2Entity(pede);
                        ped.PlanningECF = new PlanningECF();
                        ped.PlanningECF.DateECF = (DateTime)pede.DateSuggeree;
                        ped.CodePlanning = paramEntree.PlanningIndividuelFormation.CodePlanning;
                        ped = PlanningECFDetailOPManager.AjouterPlanningECFDetail(ped, pede.PlanningECF.ECF.IdECF,pede.PromotionSuggeree);
                    }
                   /* PlanningECFDetailES pedRetour = new PlanningECFDetailMappeur().Entity2ES(ped);
                    pedRetour.DejaPlanifie = true;
                    pedRetour.DateSuggeree = pedRetour.PlanningECF.DateECF;
                    lstRetour.Add(pedRetour);*/
                }
                else
                {
                    if (pede.CodePlanningECF != 0)
                    {
                        ped = new PlanningECFDetailMappeur().ES2Entity(pede);
                        if (lstAvantMiseAJour.Contains(ped))
                        {
                            //EntityConnection.GetContext().PlanningECF.Attach(pe);
                            PlanningECFDetailOPManager.SupprimerPlanningECFDetail(lstAvantMiseAJour.Find(pecfd => pecfd.IdPlanningECFDetail == ped.IdPlanningECFDetail));
                        }
                        /*  else
                          {
                             EntityConnection.GetContext().PlanningECF.Detach(pe);
                          }*/
                    }
                }
            }

          

         /*   foreach(ECF e in pif.GetListeECFAPasser()) {
                PlanningECFDetailES pede = new PlanningECFDetailES();
                pede.PlanningECF = new PlanningECFES();
                pede.PlanningECF.ECF = new ECFMappeur().Entity2ES(e);
                pede.PlanningIndividuelFormation = paramEntree.PlanningIndividuelFormation;
                pede.DateSuggeree = e.GetSuggestionDate(pif);
                pede.DejaPlanifie = false;
                lstRetour.Add(pede);
            }*/

            return new PlanifierPlanningECFDetailOut(lstRetour);
        }
    }
}
